package com.shishuo.cms.action.manage;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shishuo.cms.auth.AuthPassport;
import com.shishuo.cms.entity.UserEntity;
import com.shishuo.cms.entity.vo.JsonVo;
import com.shishuo.cms.service.SinglePageService;
import com.shishuo.cms.service.UserService;

@Controller
@RequestMapping("/admin/user")
public class UserAction extends AdminBaseAction
{
	@Autowired
	private UserService userService;
	@Autowired
	private SinglePageService singlePageService;

	@InitBinder("userEntity")
	public void initBinder(WebDataBinder binder)
	{
		binder.setFieldDefaultPrefix("userEntity.");
	}
	
	@AuthPassport
	@RequestMapping(value = "/page.htm", method = RequestMethod.GET)
	public String RolePage(@RequestParam(value = "p", defaultValue = "0") int pageNum, ModelMap modelMap) throws Exception
	{
		modelMap.put("pageVo", userService.getAllListPage(pageNum));
		return "system/user/all";
	}

	@AuthPassport
	@RequestMapping(value = "/update.htm", method = RequestMethod.GET)
	public String update(@RequestParam(value = "id") long id,
							ModelMap modelMap, HttpServletRequest request) throws Exception
	{
		UserEntity userEntity = userService.getById(id);
		modelMap.put("userEntity", userEntity);
		return "system/user/update";
	}

	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/update.json", method = RequestMethod.POST)
	public JsonVo<String> update(
									@ModelAttribute UserEntity userEntity,
									@RequestParam(value = "content", defaultValue = "") String content) throws Exception
	{
		UserEntity oldUserEntity = userService.getById(userEntity.getId());
		JsonVo<String> json = new JsonVo<String>();

		userService.update(oldUserEntity);
		json.setResult(true);
		return json;
	}

	
}
